package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/*
* 套餐管理
* */
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    /*
    * 新增套餐
    * */
    @Override
    @Transactional//开启事务管理(控制套餐表跟套餐菜品表之间的统一性)
    public void save(SetmealDTO setmealDTO) {
        //新增操作
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insert(setmeal);

        //主键返回套餐id
        Long setmealId = setmeal.getId();

        //利用get方法获取到集合setmealDishes
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null && setmealDishes.size() > 0){
            //将套餐新增过后的主键值添加到套餐_菜品表中
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            // 向套餐_菜品表中插入数据
            setmealDishMapper.insertBatch(setmealDishes);
        }

    }

    /*
    * 套餐分页查询
    * */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        /*
        Setmeal  setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealPageQueryDTO, setmeal);*/
        //返回值一定是page
       Page<SetmealVO> page =  setmealMapper.pageQuery(setmealPageQueryDTO);
        //固定返回模式
       return new PageResult(page.getTotal(),page.getResult());
    }

    /*
    * 根据id批量删除套餐
    * */
    @Override
    @Transactional//开启事务管理,同步关联的表
    public void deleteBatch(List<Long> ids) {
        //判断套餐是否处于启用状态,如果是,则不能删除
        for(Long id : ids){
            //根据Id查询套餐数据
           Setmeal setmeal =  setmealMapper.getById(id);
           if(setmeal.getStatus() == StatusConstant.ENABLE){
               //等于1表示处于启用状态，不能删除
               throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
           }
        }
        //根据id批量删除数据
        setmealMapper.deleteById(ids);

        //根据套餐id删除套餐—菜品id的关系表中的数据
        setmealDishMapper.deleteBySetmealId(ids);
    }

    /*
    * 根据套餐id查询数据
    * */
    @Override
    public SetmealVO getById(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        return setmealVO;
    }

    /*
     * 修改套餐信息
     * */
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //修改套餐信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

//根据套餐id,去套餐菜品表把数据删除
        //根据套餐id进入套餐菜品表中
        Long id = setmeal.getId();
        setmealDishMapper.deleteById(id);

        //重新插入套餐菜品表信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null && setmealDishes.size() > 0){
            setmealDishes.forEach(setmealDish -> {
                //刷新套餐_菜品表中的套餐setmealId
                setmealDish.setSetmealId(id);
            });
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    /*
    * 套餐启用停用
    * */
    @Override
    public void startOrStop(Integer status, Long id) {
        //起售套餐是时,查看套餐中的菜品是否有停用菜品,如果有,则不能起售套餐
        if(status == StatusConstant.ENABLE){
           List<Dish> dishList =  dishMapper.getBySetmealId(id);
            if(dishList != null && dishList.size() > 0){
                dishList.forEach(dish -> {
                    if(dish.getStatus() == StatusConstant.DISABLE){
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }
        //修改启用停用状态
        Setmeal setmeal  = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
